package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;


public class MainMenuScreen implements Screen, InputProcessor {

	final Egg game;
	OrthographicCamera camera;
	Rectangle start;
	Rectangle highscore;
	Rectangle quit;
	Rectangle tutorial;
	Texture startImage;
	Texture quitImage;
	Texture highscoreImage;
	Texture startClickImage;
	Texture quitClickImage;
	Texture highscoreClickImage;
	FileHandle file;
	String highscorepoint;
	public MainMenuScreen(final Egg ga)
	{
		game = ga;
		file = Gdx.files.local("data/score.txt");
		if(file.exists()){
			  highscorepoint = file.readString();
		}
		Texture.setEnforcePotImages(false);
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 800, 480);
		start = new Rectangle();
        start.x = 800/2 - 45;
        start.y = 480/2 + 128;
        start.width = 90;
        start.height = 29;
        
        quit = new Rectangle();
        quit.x = 800/2 - 45;
        quit.y = 480/2 - 38 ;
        quit.width = 90;
        quit.height = 38;
        
        highscore = new Rectangle();
        highscore.x = 800/2 - 90;
        highscore.y = 480/2 + 58;
        highscore.width = 180;
        highscore.height = 29;
        
        tutorial = new Rectangle();
        tutorial.x = 650;
        tutorial.y = 30;
        tutorial.width = 150;
        tutorial.height = 60;
        
        
        startImage = new Texture(Gdx.files.internal("start.png"));
        quitImage = new Texture(Gdx.files.internal("quit.png"));
        highscoreImage = new Texture(Gdx.files.internal("highscore.png"));
        startClickImage = new Texture(Gdx.files.internal("startclick.png"));
        quitClickImage = new Texture(Gdx.files.internal("quitclick.png"));
        highscoreClickImage = new Texture(Gdx.files.internal("highscoreclick.png"));
	}
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        
        camera.update();
        game.batch.setProjectionMatrix(camera.combined);
        
        game.batch.begin();
        //game.font.draw(game.batch, "Start", start.x, start.y);
        game.batch.draw(startImage,start.x,start.y);
        game.batch.draw(highscoreImage,highscore.x,highscore.y);
        game.batch.draw(quitImage,quit.x,quit.y);
        game.font.draw(game.batch, "Tutorial", 650, 60);
        game.font.draw(game.batch, highscorepoint, 370, 480/2 + 48);
        Vector3 vec = new Vector3();
    	vec.set(Gdx.input.getX(),Gdx.input.getY(),0);
		camera.unproject(vec);
        if(start.contains(vec.x, vec.y))
        {
        	game.batch.draw(startClickImage,start.x,start.y);
        }
        if(highscore.contains(vec.x, vec.y))
        {
        	game.batch.draw(highscoreClickImage,highscore.x,highscore.y);
        }
        if(quit.contains(vec.x, vec.y))
        {
        	game.batch.draw(quitClickImage,quit.x,quit.y);
        }
        game.batch.end();
		
        Gdx.input.setInputProcessor(this);
	}

	public void InputKey()
	{
		
	}
	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		startClickImage.dispose();
		startImage.dispose();
		quitClickImage.dispose();
		quitImage.dispose();
		highscoreClickImage.dispose();
		highscoreImage.dispose();
		Gdx.app.exit();
	}
	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		Vector3 vec = new Vector3();
    	vec.set(screenX,screenY,0);
		camera.unproject(vec);
    	if(start.contains(vec.x, vec.y))
    	{
    		game.setScreen(new GameScreen(game));
    	}
    	if(quit.contains(vec.x, vec.y))
    	{
    		dispose();
    	}
    	if(tutorial.contains(vec.x, vec.y))
    	{
    		game.setScreen(new Tutorial(game));
    	}
		return false;
	}
	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

}
